// websocket类
export let SignalSocket=(function () {
    function SignalSocket(url, uid, headr, headTime, messFun, errorFun, sendFun, onclose, Slow, reStar, reYes,Success) {
        var _this = this;
        this.url = url;
        this.uid = uid;
        this.headr = headr;
        this.headTime = headTime;
        this.messFun = messFun;
        this.errorFun = errorFun;
        this.sendFun = sendFun;
        this.onclose = onclose;
        this.Slow = Slow;
        this.reStar = reStar;
        this.reYes = reYes;
        this.socketStatuc = 0; //断线次数
        this.setTimeNull = 0; //记录获取心跳包超时次数
        this.bangbang = null; //取消time
        this.Success=Success;
        // 设置请求URL
        $.connection.hub.url = url;
        // 存储socket代理对象
        this.socket = $.connection.chatHub;
        // 收到数据消息处理程序
        this.socket.client.broadcastMessage = function (message, data) {
            _this.messFun(message, data);
        };
        // 开始与服务器链接
        $.connection.hub.start({})
            .done(function () {
            _this.sendHrad();
            _this.Success();
        });
        $.connection.hub.received(function (Msg) {
            clearTimeout(_this.Monitor);
        });
        if (this.errorFun) {
            $.connection.hub.error(function (error) {
                _this.errorFun(error);
            });
        }
        if (this.onclose) {
            $.connection.hub.disconnected(function () {
                _this.onclose();
            });
        }
        if (this.Slow) {
            $.connection.hub.connectionSlow(function () {
                _this.Slow();
            });
        }
        if (this.reStar) {
            $.connection.hub.reconnecting(function () {
                _this.reStar();
            });
        }
        if (this.reYes) {
            $.connection.hub.reconnected(function () {
                _this.reYes();
            });
        }
    }
    // 发送心跳包程序
    SignalSocket.prototype.sendHrad = function () {
        var _this = this;
        if (this.socketStatuc == 0) { //链接成功启动
            this.bangbang=setInterval(function () {
                _this.uSend(_this.headr, true);
            }, this.headTime);
        }
    };
    // 监控心跳包回复程序
    SignalSocket.prototype.getHeadMsg = function () {
        var _this = this;
        this.Monitor = setTimeout(function () {
            if (_this.setTimeNull >= 3) { //超时次数大于3，判断为网络不可用
                $.connection.hub.Stop();
            }
            _this.setTimeNull += 1;
        }, 5000);
    };
    SignalSocket.prototype.uSend = function (Msg, init) {
        if (init) {
            this.socket.server.send(this.uid, Msg); //发送心跳包
            this.getHeadMsg(); //启动监控心跳包回复定时器
        }
        else {
            // console.log("确认传输数据："+this.uid+Msg);
            this.socket.server.send(this.uid, Msg); //发送参数数据
            // console.log('我这服务器已发送');
            this.sendFun(Msg);
        }
    };
    SignalSocket.prototype.allStop=function(){
        clearInterval(this.bangbang)
        $.connection.hub.stop();
    }
    return SignalSocket;
}());